import {ref} from "vue";
import {defineStore} from "pinia";
import {getProblemService, saveProblemService, updateProblemService} from "@/api/server/problem.ts";

export const useProblemStore = defineStore('problem', () => {
    const problemId = ref('')
    const title = ref('')
    const content = ref('')
    const difficulty = ref(1)

    function resetProblem() {
        problemId.value = ''
        title.value = ''
        content.value = ''
        difficulty.value = 1
    }


    async function initProblem(id: string) {
        const problemDetail = await getProblemService(id) as any
        problemId.value = id
        title.value = problemDetail.title
        content.value = problemDetail.content
        difficulty.value = problemDetail.difficulty
    }

    interface ProblemForm {
        problemId: string
        title: string
        content: string
        difficulty: number
    }

    async function saveProblem(problemForm: ProblemForm) {
        problemId.value = await saveProblemService(problemForm) as any
        title.value = problemForm.title
        content.value = problemForm.content
        difficulty.value = problemForm.difficulty
    }

    async function updateProblem(problemForm: ProblemForm) {
        await updateProblemService(problemForm)
        title.value = problemForm.title
        content.value = problemForm.content
        difficulty.value = problemForm.difficulty
    }

    return {problemId, title, content, difficulty, resetProblem, initProblem, saveProblem, updateProblem}
})